package com.yanqu.road.server.manager.tomb.event;

import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.pb.tomb.Tomb;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.config.GoodsMgr;
import com.yanqu.road.server.manager.tomb.Position;
import com.yanqu.road.server.manager.tomb.TombMaze;
import com.yanqu.road.logic.activity.tomb.config.entity.ActivityTombEvent;
import com.yanqu.road.server.manager.tomb.entity.DbTombLog;
import com.yanqu.road.server.manager.tomb.entity.DbTombPosition;
import com.yanqu.road.server.manager.tomb.entity.DbTombUser;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;

public class Relique implements IEvent {
    @Override
    public boolean canStand(DbTombUser dbTombUser, TombMaze tombMaze, Position position) {
        DbTombPosition dbTombPosition = tombMaze.getDbTombPosition(position.getKey());
        if (dbTombPosition == null) {
            return true;
        }
        long l = dbTombPosition.acquireDigUser();
        if (dbTombUser.getUserId() == l) {
            return false;
        }
        return true;
    }

    @Override
    public boolean canThrough(DbTombUser dbTombUser, TombMaze tombMaze, Position position) {
        return canStand(dbTombUser, tombMaze, position);
    }

    @Override
    public boolean canExplore(DbTombUser dbTombUser, TombMaze tombMaze, Position position) {
        return false;
    }

    @Override
    public void detail(TombMaze tombMaze, DbTombUser dbTombUser, Position position, Tomb.PositionTemp.Builder positionTempBuilder, Tomb.TombEventExploreResp.Builder builder) {

    }

    @Override
    public void explore(TombMaze tombMaze, DbTombUser dbTombUser, DbTombPosition dbTombPosition, Position position, ActivityTombEvent activityTombEvent, Tomb.PositionTemp.Builder positionTempBuilder, Tomb.TombEventExploreResp.Builder builder) {
        positionTempBuilder.setEnum(position.getpEnum());
        positionTempBuilder.setEventEnum(position.geteType());
//        Tomb.Item.Builder itemBuilder = Tomb.Item.newBuilder();
//        itemBuilder.setId(activityTombEvent.getParam1().get(0));
//        itemBuilder.setNum(1);
//        builder.addRewardList(itemBuilder);
        Property property = new Property(activityTombEvent.getReward());
        positionTempBuilder.setIsOver(true);

        dbTombPosition.refreshUpdateTime();
//        Tomb.TombUserAddItemReq.Builder userAddItemReqBuilder = Tomb.TombUserAddItemReq.newBuilder();
//        userAddItemReqBuilder.addItemList(itemBuilder);

        tombMaze.eventDone(dbTombPosition, position);

        //通知游戏服增加道具
//        Tomb.TombUserAddItemResp.Builder userAddItemRespBuilder = Tomb.TombUserAddItemResp.newBuilder();
//        MessageHelper.sendToGameServer(dbTombUser.getUserInfo().getServerId(), GameProtocol.S_TOMB_USER_ADD_ITEM_CROSS_TO_GAME, tombMaze.getActivityId(), dbTombUser.getUserId(), userAddItemReqBuilder, userAddItemRespBuilder);


        //把遗物也放在reward里，游戏服统一处理
        property.addProperty(activityTombEvent.getParam1().get(0), BigInteger.valueOf(1));
        tombMaze.addProperty(property, dbTombUser);
        builder.setRewards(PropertyHelper.parsePropertyToString(property));


        GoodsInfo goodsInfo = GoodsMgr.getGoodsInfo(activityTombEvent.getParam1().get(0));
        if (goodsInfo.getQuality() == 5) {
            //日志
            DbTombLog dbTombLog = DbTombLog.newInstance();
            dbTombLog.setUid(String.valueOf(dbTombUser.getUserId()));
            dbTombLog.setFloor(tombMaze.getFloor());
            dbTombLog.setFromUserId(dbTombUser.getUserId());
            dbTombLog.setLogEnum(Tomb.TombLogEnum.TombLogEnum_Relique_VALUE);
            dbTombLog.setEventId(activityTombEvent.getId());
            dbTombLog.setRewards(builder.getRewards());
            dbTombLog.setValue1(activityTombEvent.getParam1().get(0));
            dbTombLog.setX(position.getRow());
            dbTombLog.setY(position.getColumn());
            dbTombLog.setDescription("发现了地宫遗物");
            dbTombLog.setGroupId(dbTombUser.getGroupId());
            tombMaze.getActivity().getTombLog().addDbTombLog(dbTombLog, false);


            //日志
            DbTombLog dbTombLog1 = DbTombLog.newInstance();
            dbTombLog1.setUid(String.valueOf(tombMaze.getActivityId()));
            dbTombLog1.setFloor(tombMaze.getFloor());
            dbTombLog1.setFromUserId(dbTombUser.getUserId());
            dbTombLog1.setLogEnum(Tomb.TombLogEnum.TombLogEnum_Relique_VALUE);
            dbTombLog1.setEventId(activityTombEvent.getId());
            dbTombLog1.setRewards(builder.getRewards());
            dbTombLog1.setValue1(activityTombEvent.getParam1().get(0));
            dbTombLog1.setX(position.getRow());
            dbTombLog1.setY(position.getColumn());
            dbTombLog1.setGroupId(dbTombUser.getGroupId());
            dbTombLog1.setDescription("发现了地宫遗物");
            tombMaze.getActivity().getTombLog().addDbTombLog(dbTombLog1, true);
        }


    }

    @Override
    public void display(DbTombUser dbTombUser, TombMaze tombMaze, Position position, Tomb.PositionTemp.Builder p) {
        p.setEnum(Tomb.PositionEnum.POSITION_EVENT_DONE_VALUE);
    }


    @Override
    public void info(TombMaze tombMaze, DbTombUser dbTombUser, DbTombPosition dbTombPosition, Position position, ActivityTombEvent activityTombEvent, Tomb.PositionTemp.Builder positionTempBuilder, Tomb.TombEventInfoResp.Builder builder) {
        positionTempBuilder.setEnum(Tomb.PositionEnum.POSITION_EVENT_DONE_VALUE);

    }

    @Override
    public void touch(TombMaze tombMaze, DbTombUser dbTombUser, DbTombPosition dbTombPosition, Position position, ActivityTombEvent activityTombEvent, Tomb.PositionTemp.Builder positionTempBuilder, Tomb.TombEventTouchResp.Builder builder, Tomb.TombEventTouchReq req) {

    }
}
